package cn.android666.audiorecorder.objectpool

import org.apache.commons.pool2.BasePooledObjectFactory
import org.apache.commons.pool2.PooledObject
import org.apache.commons.pool2.impl.DefaultPooledObject
import java.nio.ByteBuffer

class ByteBufferFactory(private val bufferSize: Int) : BasePooledObjectFactory<ByteBuffer>() {

    override fun create(): ByteBuffer = ByteBuffer.allocateDirect(bufferSize)
    override fun wrap(obj: ByteBuffer): PooledObject<ByteBuffer> = DefaultPooledObject(obj)
    override fun passivateObject(p: PooledObject<ByteBuffer>) { p.`object`.clear() }

}